package com.notifier.notifier;

import cn.hutool.core.util.StrUtil;
import com.notifier.event.ErrorEvent;
import com.notifier.utils.DingSendUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 钉钉通知器
 *
 * @author Amadeus
 * @date 2025/10/14
 */
@Slf4j
@Component
public class DingNotifier implements IErrorNotifier {
    
    @Autowired
    private DingSendUtil dingSendUtil;
    
    @Override
    public void notify(ErrorEvent event) {
        try {
            String message = event.getFullErrorMessage();
            
            // 钉钉消息有长度限制，需要分段发送
            String[] segments = StrUtil.split(message, 10000);
            for (String segment : segments) {
                dingSendUtil.dingRequest(segment);
            }
            
            log.debug("钉钉通知发送成功，应用：{}", event.getApplicationName());
        } catch (Exception e) {
            log.warn("钉钉通知发送失败", e);
        }
    }
    
    @Override
    public String getName() {
        return "DingNotifier";
    }
    
    @Override
    public boolean isEnabled() {
        // 始终启用钉钉通知
        return true;
    }
}

